package com.imcrl.component
{
	import com.imcrl.component.itemRender.IListItemRender;
	import com.imcrl.core.ChooseState;
	import com.imcrl.core.IFactory;
	import com.imcrl.core.IRenderable;
	import com.imcrl.core.imcrl_internal;
	import com.imcrl.event.ListItemEvent;
	import com.imcrl.layout.TileLayout;
	import com.imcrl.manager.Pool;
	import com.imcrl.manager.RenderManager;
	
	import starling.display.DisplayObject;
	import starling.display.DisplayObjectContainer;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.EventDispatcher;
	import starling.events.ResizeEvent;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	import starlingui.component.itemRender.IconItemRender;
	
	
	/**
	 * 内部列表(不让直接被创建)
	 * @author crl
	 * 
	 */	
	public class PageList extends Sprite implements IDisplayList ,IRenderable
	{
		protected var _layout:ILayoutable;
		
		protected var _pool:Pool;
		
		protected var _data:*;
		public function PageList(factory:IFactory)
		{
			super();
			
			pool=new Pool(factory);
		}
		
		public function set layout(value:ILayoutable):void{
			_layout=value;
		}
		
		public function displayList(value:*):void
		{
			_data=value;
			selectedItem=null;
			invalidate();
		}
		
		public function get data():*{
			return _data;
		}
		
		
		public function invalidate():void{
			RenderManager.add(this);
		}
		
		public function rende():void{
			
			_layout.clear();
			
			var len:int=numChildren;	
			var item:IListItemRender;
			
			var dataLen:int=_data.length;
			
			for(var i:int=0;i<len;i++){
				item=getChildAt(i) as IListItemRender;
				
				if(i>=dataLen){
					bindEnabled(item,false);
					_pool.add(item);
					removeChild(item as DisplayObject);
					i--;
					len--;
					continue;
				}
				
				item.index=i;
				item.data=_data[i];
				item.chooseState=ChooseState.UNSELECT;
				
				_layout.layout(item,i);
			}
			
			for(;i<dataLen;i++){
				
				item=_pool.get() as IListItemRender;
				bindEnabled(item,true);
				addChild(item as DisplayObject);
				item.index=i;
				item.data=_data[i];
				item.chooseState=ChooseState.UNSELECT;
				
				_layout.layout(item,i);
			}
			
			
			if(hasEventListener(Event.COMPLETE)){
				dispatchEvent(new Event(Event.COMPLETE));
			}
			
		}
		
		
		
		protected function clear():void{
			var item:IListItemRender;
			while(this.numChildren){
				item=removeChildAt(0) as IListItemRender;
				bindEnabled(item,false);
				
				_pool.add(item);
			}
		}
		
		imcrl_internal function removeItem(value:IListItemRender,dispose:Boolean=false):Boolean{
			var index:int=this.getChildIndex(value as DisplayObject);
			if(index==-1)return false;
			
			bindEnabled(value,false);
			
			this.removeChildAt(index,dispose);
			return true;
		}
		
		protected function bindEnabled(item:IListItemRender,enabled:Boolean):void{
			if(item is EventDispatcher == false)return;
			var eventDispatcher:EventDispatcher=item as EventDispatcher;
			if(enabled){
				eventDispatcher.addEventListener(Event.TRIGGERED,triggeredHandler);
				eventDispatcher.addEventListener(ResizeEvent.RESIZE,resizeHandler);
			}else{
				eventDispatcher.removeEventListener(Event.TRIGGERED,triggeredHandler);
				eventDispatcher.addEventListener(ResizeEvent.RESIZE,resizeHandler);
			}
		}
		
		protected function resizeHandler(event:ResizeEvent):void{
			if(hasEventListener(event.type))dispatchEvent(event);
		}
		
		
		protected var _selectedItem:IListItemRender;
		protected function triggeredHandler(event:Event):void{
			var old:IListItemRender=selectedItem;
			selectedItem=event.currentTarget as IListItemRender;
			
			if(hasEventListener(ListItemEvent.SELECTED) && (_allowRepeat || old !=_selectedItem)){
				dispatchEvent(new ListItemEvent(ListItemEvent.SELECTED));
			}
		}
		
		private var _allowRepeat:Boolean;
		public function set allowRepeat(value:Boolean):void{
			_allowRepeat=value;
		}
		
		public function get selectedItem():IListItemRender{
			return _selectedItem;
		}
		
		public function set selectedItem(value:IListItemRender):void{
			var isChange:Boolean=_selectedItem !=value
			
			if(_selectedItem){
				if(!isChange){
					return;
				}
				
				_selectedItem.chooseState=ChooseState.UNSELECT;
			}
			
			_selectedItem=value;
			if(_selectedItem){
				_selectedItem.chooseState=ChooseState.SELECT;
			}
			
			if(isChange && hasEventListener(ListItemEvent.CHANGE)){
				dispatchEvent(new ListItemEvent(ListItemEvent.CHANGE));
			}
		}
		
		
		public function set selectedIndex(value:int):void{
			if(value<0){
				selectedItem=null;
				return;
			}
			if(value>numChildren){
				value=numChildren;
			}
			
			selectedItem=getChildAt(value) as IListItemRender;
		}
		
		public function get selectedIndex():int{
			if(selectedItem==null){
				return -1;
			}
			return getChildIndex(selectedItem as DisplayObject);
		}
		
		
		override public function dispose():void
		{
			var len:int=this.numChildren;
			var item:IListItemRender;
			for(var i:int=0;i<len;i++){
				item=getChildAt(i) as IListItemRender;
				bindEnabled(item,false);
				item.dispose();
			}
			
			pool=null;
			
			super.dispose();
		}
		
		public function awaken():void
		{
			var len:int=this.numChildren;
			for(var i:int=0;i<len;i++){
				(getChildAt(i) as IListItemRender).awaken();
			}
		}
		
		public function sleep():void
		{
			var len:int=this.numChildren;
			for(var i:int=0;i<len;i++){
				(getChildAt(i) as IListItemRender).sleep();
			}
		}
		
		public function set pool(value:Pool):void{
			_pool=value;
		}
		
	}
}